package com.slfx.open.api.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 请求包装filter
 * 
 * @ClassName: MyProxyFilter
 */
@WebFilter(filterName="customFilter",urlPatterns="/*")
@Component
public class MyProxyFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		String contentType = request.getContentType();
		if (contentType != null && contentType.indexOf("json") != -1) {
			HttpServletRequest httpRequest = (HttpServletRequest) request;
			//包装一下request,requst中参数只能获取一次，再次获取则为空
			request = new MyHttpServletRequestWrapper(httpRequest);
		}
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {

	}

}
